package model
{
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.net.NetConnection;
	import flash.net.Responder;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import flashx.textLayout.events.UpdateCompleteEvent;
	
	import model.vo.DataVO;
	import model.vo.ViewVO;
	
	import org.osmf.layout.LayoutAttributesFacet;
	import org.puremvc.as3.core.View;
	import org.puremvc.as3.interfaces.IProxy;
	import org.puremvc.as3.patterns.proxy.Proxy;
	
	import view.LoginView;
	import view.MenuView;
	import view.PopupView;
	import view.ProgressView;

	public class DataProxy extends Proxy implements IProxy
	{
		public static const NAME:String 				= "DataProxy";
		
		private var dataLoader:URLLoader;
		
		private var netConnection:NetConnection;
		
		private var propertiesDataLoaded:Boolean;
		private var menuDataLoaded:Boolean;
		
		private var contentNum:int;
		private var contentLoadedNum:int;
		
		public function DataProxy()
		{
			super( NAME, new DataVO() );
			
			dataLoader = new URLLoader();
		}
		
		public function applicationDataGet():void
		{
			sendNotification( ProgressView.SHOW, { txt:"DB Connection..." } );
//			viewProxy.vo.progressView.show("DB Connection...");
			netConnection = new NetConnection();
			netConnection.connect("http://brunoperry.net/ini.php");
//			netConnection.connect("http://localhost/");
			var res:Responder = new Responder( applicationDataGetCompleteHandler, onError);
			netConnection.call("Users.getInitData", res);
		}
		
		public function applicationImagesGet( arr:Array ):void
		{
			var i:uint;
			for( i = 0; i < arr.length; i++ )
			{
				
			}
		}
		
		public function currentViewDataSet( index:int ):void
		{
			vo.currentViewData = vo.contentViewData[ index ];
		}
		
		public function validateUser( info:Object ):void
		{
			sendNotification( ProgressView.SHOW, { txt:"Validating User..." } );
			var res:Responder = new Responder( validateUserCompleteHandler, onError);
			netConnection.call("Users.getUserData", res, info );
		}
		
		private function applicationDataGetCompleteHandler( e:Object ):void
		{
			viewProxy.setAppProperties( e[0][0] );
			vo.menuViewData 	= e[1];
			vo.contentViewData 	= e[2];
			messageProxy.setMessages( e[3][0] );
			
			sendNotification( ApplicationFacade.DATA_READY );
			
			sendNotification( ProgressView.HIDE, { txt:"DONE" } );
		}
		
		private function validateUserCompleteHandler( e:Object ):void
		{
			var call:String;
			
			if( e.length == 0 )
			{
				call = "No user found...";
				sendNotification( LoginView.WRONG_LOGIN );
				
				messageProxy.vo.userValidationError.headerTxt 			= viewProxy.vo.errorText;
				messageProxy.vo.userValidationError.headerDetailTxt 	= call;
				
				sendNotification( PopupView.SHOW, messageProxy.vo.userValidationError );
			}
			else
			{
				call = "User validated...";
				sendNotification( LoginView.SUCCESS_LOGIN, e[0] );
			}
			
			sendNotification( ProgressView.HIDE, { txt:call } );
		}
		
		private function onError( e:Object ):void
		{
			trace(e);
		}
		
		public function get vo():DataVO
		{
			return data as DataVO;
		}
		
		private function get viewProxy():ViewProxy
		{
			return facade.retrieveProxy( ViewProxy.NAME ) as ViewProxy;
		}
		
		private function get messageProxy():MessageProxy
		{
			return facade.retrieveProxy( MessageProxy.NAME ) as MessageProxy;
		}
	}
}